package com.olt.config;

import com.alibaba.fastjson.JSON;
import com.olt.result.ErrorEnum;
import com.olt.result.Result;
import org.apache.shiro.web.filter.authc.UserFilter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.PrintWriter;

public class MyUserFilter extends UserFilter {

    /**
     * 拦截时返回  JSON，而不是跳转到一个loginUrl
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        Result<String> r=new Result<>();
        r.setError(ErrorEnum.NOT_LOGIN);
        out.write(JSON.toJSONString(r));
        return false;
    }

}
